<template>
	<view class="page">
		<!-- #ifdef H5 -->
		<topVue title="积分充值" rightIcon="server-fill" :isBack="true"></topVue>
		<!-- #endif -->

		<view class="main-content">
			<scroll-view class="content" scroll-y>

				<u-subsection :list="tabs" :current="current" button-color=" #3B82F6" active-color="#fff"
					bg-color="#f5f5f5" @change="change"></u-subsection>
				<!-- AI短视频积分充值 -->
				<template v-if="current===0">
					<view class="label" style="padding-top: 40rpx;">AI短视频积分充值</view>
					<view class="price">100元 = 1350积分</view>
					<view class="price">单笔充值金额不能大于50000元</view>
					<view class="price">最低充值 100 元，且金额必须为 100 的倍数。</view>
					<view class="price">消耗：半分钟消耗30积分, 1分钟消耗60AI视频积分</view>
					<view class="h2">充值金额</view>
					<input type="number" placeholder="请输入充值" v-model="videoNum" maxlength="5" @input="limitation(videoNum,current)" />
					<view class="flex-between margin30">
						<text style="color: #866D6F; line-height: 1;">充值前积分</text>
						<text style="color: #000; font-weight: 600; line-height: 1;">{{ aiVideoPoints }}积分</text>
					</view>
					<view class="flex-between margin30">
						<text style="color: #866D6F; line-height: 1;">充值后积分</text>
						<text style="color: #000; font-weight: 600; line-height: 1;">{{ calculatePoints(videoNum,current) + aiVideoPoints }}积分</text>
					</view>
					<view class="flex-between margin50">
						<view class="label">总金额</view>
						<text
							style="color: #2563EB; font-weight: 600; font-size: 32rpx;">{{ calculate(videoNum) | number(2) }}元</text>
					</view>
				</template>

				<!-- AI微信积分充值 -->
				<template v-if="current===1">
					<view class="label" style="padding-top: 40rpx;">AI微信积分充值</view>
					<view class="price">100元 = 50000积分</view>
					<view class="price">单笔充值金额不能大于50000元</view>
					<view class="price">最低充值 100 元，且金额必须为 100 的倍数。</view>
					<view class="price">消耗：每20字左右对话消耗20AI对话积分</view>
					<view class="h2">充值金额</view>
					<input type="number" placeholder="请输入充值金额" v-model="infoNum"  maxlength="5" @input="limitation(infoNum,current)" />
					<view class="flex-between margin30">
						<text style="color: #866D6F; line-height: 1;">充值前积分</text>
						<text style="color: #000; font-weight: 600; line-height: 1;">{{ aiWechatPoints }}积分</text>
					</view>
					<view class="flex-between margin30">
						<text style="color: #866D6F; line-height: 1;">充值后积分</text>
						<text style="color: #000; font-weight: 600; line-height: 1;">{{ calculatePoints(infoNum,current) + aiWechatPoints }}积分</text>
					</view>

					<view class="flex-between margin50">
						<view class="label">总金额</view>
						<text
							style="color: #2563EB; font-weight: 600; font-size: 32rpx;">{{ calculate(infoNum) | number(2) }}元</text>
					</view>
				</template>

				<view class="btn margin40" @click="chargeFun()">立即充值</view>
				<!-- <view class="remarks text-center">充值金额不可低于100元</view> -->
			</scroll-view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				videoNum: 0,
				infoNum: 0,
				tabs: [{
					name: "AI短视频积分充值"
				}, {
					name: "AI微信积分充值"
				}],
				current: 0,
				aiVideoPoints: 0,
				aiWechatPoints: 0,
			}
		},
		async onLoad() {
			await this.getPoint()
		},
		methods: {
			async getPoint() {
				const res = await this.$request("/aiet-user-center/user/points/summary", {}, "GET")
				if (res.code === 200) {
					this.aiVideoPoints = res.data.aiVideoPoints
					this.aiWechatPoints = res.data.aiWechatPoints
				}
			},
			limitation(many,type) {
				if (many>50000) {
					this.videoNum = type===0?50000:this.videoNum
					this.infoNum = type===1?50000:this.infoNum
					return this.$u.toast("单次充值金额不能大于50000元")
				}
			},
			calculate(many) {
				let hundredBlocks = Math.floor(many / 100);
				return hundredBlocks * 100;
			},
			calculatePoints(many, type) {
				// 每100元等于5000的积分
				let pointsPer100Units = 1350;
				if (type === 1) {
					pointsPer100Units = 5000;
				}
				// 计算总共的100元
				let hundredBlocks = Math.floor(many / 100);
				// 计算总积分
				return hundredBlocks * pointsPer100Units;
			},
			async chargeFun() {
				if (this.current === 0 && !this.videoNum) return this.$u.toast("请输入充值金额")
				if (this.current === 0 && this.videoNum < 100) return this.$u.toast("充值金额不低于100元")
				if (this.current === 1 && !this.infoNum) return this.$u.toast("请输入充值金额")
				if (this.current === 1 && this.infoNum < 100) return this.$u.toast("充值金额不低于100元")
				const res = await this.$request("/aiet-user-center/user/orders/pay", {
					// productName: "购买AI"+this.current===0?"视频":"微信"+this.current===0?calculatePoints(this.videoNum,this.current):0,+"+""积分",
					productName: `购买AI${this.current===0?"视频":"微信"}${this.current===0?this.calculatePoints(this.videoNum,this.current):this.calculatePoints(this.infoNum,this.current)}积分`,
					// orderAmount: this.selectObj.price,
					orderAmount: this.current===0 ? this.calculate(this.videoNum) : this.calculate(this.infoNum),
					platform: "数字人-H5",
					productType: 1,
					videoPoints: this.current===0 ? this.calculatePoints(this.videoNum, this.current):0,
					wechatPoints: this.current=== 1 ? this.calculatePoints(this.infoNum, this.current):0,
					manufacturer: 1
				}, "POST", "application/json")
				if (res.code === 200) {
					console.log(res.data.payUrl)
					// #ifdef H5
					window.location.href = res.data.payUrl
					// #endif
				} else {
					this.$u.toast(res.msg)
				}
			},
			change(e) {
				this.current = e
			}
		}
	}
</script>

<style lang="less" scoped>
	.main-content {
		width: 100%;
		height: calc(100vh - var(--window-bottom) - var(--window-top) - 92rpx);
		padding: 30rpx;
		box-sizing: border-box;
		background: linear-gradient(to bottom, #10B981, #3B82F6);

		.content {
			width: 100%;
			height: calc(100vh - var(--window-bottom) - var(--window-top) - 152rpx);
			background: #fff;
			border-radius: 12rpx;
			padding: 30rpx;
			box-sizing: border-box;

			.label {
				font-weight: 600;
				font-size: 32rpx;
				color: #000;
			}

			.price {
				color: #866D6F;
				padding-top: 10rpx;
			}

			.h2 {
				color: #000;
				font-size: 30rpx;
				padding: 30rpx 0 15rpx 0;
			}

			input {
				width: 100%;
				height: 80rpx;
				border: 1rpx solid #F3F4F6;
				border-radius: 10rpx;
				display: block;
				padding-left: 20rpx;
				font-size: 28rpx;
			}

			.btn {
				width: 100%;
				height: 80rpx;
				background: linear-gradient(to right, #10B981, #3B82F6);
				line-height: 80rpx;
				text-align: center;
				border-radius: 12rpx;
			}

			.remarks {
				font-size: 28rpx;
				margin-top: 20rpx;
				color: #8A8EAA;
			}
		}
	}
</style>
